package com.woniu.market_stall_web.socket;


import com.woniu.market_stall_web.entity.User;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Component
public class PushHandShakeIntercepter implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest,
                                   ServerHttpResponse serverHttpResponse,
                                   WebSocketHandler webSocketHandler,
                                   Map<String, Object> map) throws Exception {
        System.out.println("握手成功!!!");
        ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) serverHttpRequest;

        HttpServletRequest request = servletServerHttpRequest.getServletRequest();
        User user = (User) request.getSession().getAttribute("userInfo");

        System.out.println("登录用户 :"+user.getUsername());
        map.put("account",user.getUsername());

        String roomId = request.getParameter("roomId");
        System.out.println("roomId:"+roomId);

        map.put("roomId",roomId);
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }
}
